package com.hospital.pet.love.lovepethospital.controller;

import com.hospital.pet.love.lovepethospital.domain.Register;
import com.hospital.pet.love.lovepethospital.dto.register.AddRegisterDto;
import com.hospital.pet.love.lovepethospital.dto.register.GetRegisterDto;
import com.hospital.pet.love.lovepethospital.exp.ShawCustomException;
import com.hospital.pet.love.lovepethospital.resp.Result;
import com.hospital.pet.love.lovepethospital.resp.ResultCodeEnum;
import com.hospital.pet.love.lovepethospital.service.RegisterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RoselleShaw
 * @create 2024-04-26 11:12
 */
@RestController
@Tag(name = "挂号管理")
@RequestMapping("/register")
public class RegisterController {

    @Resource
    private RegisterService registerService;

    @PostMapping("/save")
    @Operation(summary = "预约或修改挂号")
    public Result<Void> save(@RequestBody AddRegisterDto addRegisterDto) {
        registerService.save(addRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/cancel/{id}")
    @Operation(summary = "取消挂号")
    public Result<Void> cancel(@PathVariable Integer id) {
        if (registerService.getById(id) == null) {
            throw new ShawCustomException(ResultCodeEnum.REGISTER_NOT_EXIST);
        }
        Register register = new Register();
        register.setRegisterId(id);
        register.setState(2);
        registerService.updateById(register);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/get-by-phone")
    @Operation(summary = "获取所有挂号（客户）")
    public Result<List<Register>> getByPhone(@RequestBody GetRegisterDto getRegisterDto) {
        List<Register> registers = registerService.getByPhone(getRegisterDto);
        return Result.build(registers, ResultCodeEnum.SUCCESS);
    }
}
